"""
余额分账相关模型
"""

from jlpay.model.base import OrgBaseReq, BaseResponse


# ==================== 余额查询 ====================
class BalanceQueryRequest(OrgBaseReq):
    """余额查询请求"""
    
    API_URL = "/open/ledger/balance/query"
    
    def __init__(self):
        """初始化余额查询请求"""
        super().__init__()
        self.org_code = None
        self.mch_id = None
        self.account_type = None
    
    def set_org_code(self, org_code):
        """
        设置机构代码
        
        Args:
            org_code: 机构代码
            
        Returns:
            BalanceQueryRequest实例
        """
        self.org_code = org_code
        return self
    
    def set_mch_id(self, mch_id):
        """
        设置商户号
        
        Args:
            mch_id: 商户号
            
        Returns:
            BalanceQueryRequest实例
        """
        self.mch_id = mch_id
        return self
    
    def set_account_type(self, account_type):
        """
        设置账户类型
        
        Args:
            account_type: 账户类型
            
        Returns:
            BalanceQueryRequest实例
        """
        self.account_type = account_type
        return self


class BalanceQueryResponse(BaseResponse):
    """余额查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化余额查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.org_code = None
        self.mch_id = None
        self.account_type = None
        self.balance = None
        self.available_balance = None
        
        if response_dict:
            self.org_code = response_dict.get('org_code')
            self.mch_id = response_dict.get('mch_id')
            self.account_type = response_dict.get('account_type')
            self.balance = response_dict.get('balance')
            self.available_balance = response_dict.get('available_balance')


# ==================== 余额分账 ====================
class BalanceSplitRequest(OrgBaseReq):
    """余额分账请求"""
    
    API_URL = "/open/ledger/balance/split"
    
    def __init__(self):
        """初始化余额分账请求"""
        super().__init__()
        self.org_code = None
        self.mch_id = None
        self.out_order_no = None
        self.total_amount = None
        self.receivers = None
    
    def set_org_code(self, org_code):
        """
        设置机构代码
        
        Args:
            org_code: 机构代码
            
        Returns:
            BalanceSplitRequest实例
        """
        self.org_code = org_code
        return self
    
    def set_mch_id(self, mch_id):
        """
        设置商户号
        
        Args:
            mch_id: 商户号
            
        Returns:
            BalanceSplitRequest实例
        """
        self.mch_id = mch_id
        return self
    
    def set_out_order_no(self, out_order_no):
        """
        设置外部订单号
        
        Args:
            out_order_no: 外部订单号
            
        Returns:
            BalanceSplitRequest实例
        """
        self.out_order_no = out_order_no
        return self
    
    def set_total_amount(self, total_amount):
        """
        设置分账总金额
        
        Args:
            total_amount: 分账总金额
            
        Returns:
            BalanceSplitRequest实例
        """
        self.total_amount = total_amount
        return self
    
    def set_receivers(self, receivers):
        """
        设置分账接收方列表
        
        Args:
            receivers: 分账接收方列表
            
        Returns:
            BalanceSplitRequest实例
        """
        self.receivers = receivers
        return self


class BalanceSplitResponse(BaseResponse):
    """余额分账响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化余额分账响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.org_code = None
        self.out_order_no = None
        self.order_no = None
        self.total_amount = None
        self.state = None
        self.receivers = None
        
        if response_dict:
            self.org_code = response_dict.get('org_code')
            self.out_order_no = response_dict.get('out_order_no')
            self.order_no = response_dict.get('order_no')
            self.total_amount = response_dict.get('total_amount')
            self.state = response_dict.get('state')
            
            # 将 receivers 字典列表转换为 Receiver 对象列表
            receivers_data = response_dict.get('receivers', [])
            self.receivers = []
            for receiver_dict in receivers_data:
                receiver = Receiver(receiver_dict)
                self.receivers.append(receiver)
    
    def to_dict(self):
        """
        将响应对象转换为字典，正确处理receivers字段
        
        Returns:
            字典对象
        """
        data = super().to_dict()
        
        # 正确处理receivers字段
        if self.receivers:
            data['receivers'] = [receiver.to_dict() for receiver in self.receivers]
        
        return data


class Receiver:
    """接收方信息"""
    
    def __init__(self, receiver_dict=None):
        """
        初始化接收方信息
        
        Args:
            receiver_dict: 接收方信息字典
        """
        self.rev_mch_id = None
        self.amount = None
        self.description = None
        self.result = None
        self.fail_reason = None
        self.detail_id = None
        self.create_time = None
        self.finish_time = None
        
        if receiver_dict:
            self.rev_mch_id = receiver_dict.get('rev_mch_id')
            self.amount = receiver_dict.get('amount')
            self.description = receiver_dict.get('description')
            self.result = receiver_dict.get('result')
            self.fail_reason = receiver_dict.get('fail_reason')
            self.detail_id = receiver_dict.get('detail_id')
            self.create_time = receiver_dict.get('create_time')
            self.finish_time = receiver_dict.get('finish_time')
    
    def to_dict(self):
        """
        将接收方信息转换为字典
        
        Returns:
            字典对象
        """
        return {
            'rev_mch_id': self.rev_mch_id,
            'amount': self.amount,
            'description': self.description,
            'result': self.result,
            'fail_reason': self.fail_reason,
            'detail_id': self.detail_id,
            'create_time': self.create_time,
            'finish_time': self.finish_time
        }


# ==================== 余额分账查询 ====================
class BalanceSplitQueryRequest(OrgBaseReq):
    """余额分账查询请求"""
    
    API_URL = "/open/ledger/balance/split/query"
    
    def __init__(self):
        """初始化余额分账查询请求"""
        super().__init__()
        self.org_code = None
        self.out_order_no = None
        self.order_no = None
    
    def set_org_code(self, org_code):
        """
        设置机构代码
        
        Args:
            org_code: 机构代码
            
        Returns:
            BalanceSplitQueryRequest实例
        """
        self.org_code = org_code
        return self
    
    def set_out_order_no(self, out_order_no):
        """
        设置外部订单号
        
        Args:
            out_order_no: 外部订单号
            
        Returns:
            BalanceSplitQueryRequest实例
        """
        self.out_order_no = out_order_no
        return self
    
    def set_order_no(self, order_no):
        """
        设置嘉联分账单号
        
        Args:
            order_no: 嘉联分账单号
            
        Returns:
            BalanceSplitQueryRequest实例
        """
        self.order_no = order_no
        return self


class BalanceSplitQueryResponse(BaseResponse):
    """余额分账查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化余额分账查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.org_code = None
        self.out_order_no = None
        self.order_no = None
        self.total_amount = None
        self.state = None
        self.receivers = None
        
        if response_dict:
            self.org_code = response_dict.get('org_code')
            self.out_order_no = response_dict.get('out_order_no')
            self.order_no = response_dict.get('order_no')
            self.total_amount = response_dict.get('total_amount')
            self.state = response_dict.get('state')
            
            # 将 receivers 字典列表转换为 Receiver 对象列表
            receivers_data = response_dict.get('receivers', [])
            self.receivers = []
            for receiver_dict in receivers_data:
                receiver = Receiver(receiver_dict)
                self.receivers.append(receiver)
    
    def to_dict(self):
        """
        将响应对象转换为字典，正确处理receivers字段
        
        Returns:
            字典对象
        """
        data = super().to_dict()
        
        # 正确处理receivers字段
        if self.receivers:
            data['receivers'] = [receiver.to_dict() for receiver in self.receivers]
        
        return data


# ==================== 余额退款 ====================
class BalanceRefundRequest(OrgBaseReq):
    """余额退款请求"""
    
    API_URL = "/open/ledger/balance/refund"
    
    def __init__(self):
        """初始化余额退款请求"""
        super().__init__()
        self.org_code = None
        self.mch_id = None
        self.out_order_no = None
        self.out_return_no = None
        self.rev_mch_id = None
        self.amount = None
        self.description = None
    
    def set_org_code(self, org_code):
        """
        设置机构代码
        
        Args:
            org_code: 机构代码
            
        Returns:
            BalanceRefundRequest实例
        """
        self.org_code = org_code
        return self
    
    def set_mch_id(self, mch_id):
        """
        设置商户号
        
        Args:
            mch_id: 商户号
            
        Returns:
            BalanceRefundRequest实例
        """
        self.mch_id = mch_id
        return self
    
    def set_out_order_no(self, out_order_no):
        """
        设置外部订单号
        
        Args:
            out_order_no: 外部订单号
            
        Returns:
            BalanceRefundRequest实例
        """
        self.out_order_no = out_order_no
        return self
    
    def set_out_return_no(self, out_return_no):
        """
        设置外部退款号
        
        Args:
            out_return_no: 外部退款号
            
        Returns:
            BalanceRefundRequest实例
        """
        self.out_return_no = out_return_no
        return self
    
    def set_rev_mch_id(self, rev_mch_id):
        """
        设置接收方商户号
        
        Args:
            rev_mch_id: 接收方商户号
            
        Returns:
            BalanceRefundRequest实例
        """
        self.rev_mch_id = rev_mch_id
        return self
    
    def set_amount(self, amount):
        """
        设置退款金额
        
        Args:
            amount: 退款金额
            
        Returns:
            BalanceRefundRequest实例
        """
        self.amount = amount
        return self
    
    def set_description(self, description):
        """
        设置退款描述
        
        Args:
            description: 退款描述
            
        Returns:
            BalanceRefundRequest实例
        """
        self.description = description
        return self


class BalanceRefundResponse(BaseResponse):
    """余额退款响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化余额退款响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.org_code = None
        self.out_order_no = None
        self.order_no = None
        self.out_return_no = None
        self.return_no = None
        self.amount = None
        self.description = None
        self.result = None
        self.fail_reason = None
        self.create_time = None
        self.finish_time = None
        
        if response_dict:
            self.org_code = response_dict.get('org_code')
            self.out_order_no = response_dict.get('out_order_no')
            self.order_no = response_dict.get('order_no')
            self.out_return_no = response_dict.get('out_return_no')
            self.return_no = response_dict.get('return_no')
            self.amount = response_dict.get('amount')
            self.description = response_dict.get('description')
            self.result = response_dict.get('result')
            self.fail_reason = response_dict.get('fail_reason')
            self.create_time = response_dict.get('create_time')
            self.finish_time = response_dict.get('finish_time')


# ==================== 余额退款查询 ====================
class BalanceRefundQueryRequest(OrgBaseReq):
    """余额退款查询请求"""
    
    API_URL = "/open/ledger/balance/refund/query"
    
    def __init__(self):
        """初始化余额退款查询请求"""
        super().__init__()
        self.org_code = None
        self.out_return_no = None
    
    def set_org_code(self, org_code):
        """
        设置机构代码
        
        Args:
            org_code: 机构代码
            
        Returns:
            BalanceRefundQueryRequest实例
        """
        self.org_code = org_code
        return self
    
    def set_out_return_no(self, out_return_no):
        """
        设置外部退款号
        
        Args:
            out_return_no: 外部退款号
            
        Returns:
            BalanceRefundQueryRequest实例
        """
        self.out_return_no = out_return_no
        return self


class BalanceRefundQueryResponse(BaseResponse):
    """余额退款查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化余额退款查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.org_code = None
        self.out_order_no = None
        self.order_no = None
        self.out_return_no = None
        self.return_no = None
        self.amount = None
        self.description = None
        self.result = None
        self.fail_reason = None
        self.create_time = None
        self.finish_time = None
        
        if response_dict:
            self.org_code = response_dict.get('org_code')
            self.out_order_no = response_dict.get('out_order_no')
            self.order_no = response_dict.get('order_no')
            self.out_return_no = response_dict.get('out_return_no')
            self.return_no = response_dict.get('return_no')
            self.amount = response_dict.get('amount')
            self.description = response_dict.get('description')
            self.result = response_dict.get('result')
            self.fail_reason = response_dict.get('fail_reason')
            self.create_time = response_dict.get('create_time')
            self.finish_time = response_dict.get('finish_time') 